package com.api.shihua.action;

import com.api.shihua.cmd.RewardBaseService;
import com.api.shihua.entity.Reward;
import com.api.shihua.util.BaseUtil;
import com.api.shihua.util.LogTool;
import jxl.read.biff.Record;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.action.BaseAction;

import java.text.SimpleDateFormat;
import java.util.*;

/*
    总经理一级写入建模

 */
public class RewardLeve1Action extends BaseAction {
    private final static LogTool log = new LogTool("/log/zjl/",false);
    @Override
    public String execute(RequestInfo requestInfo) {

        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate=sim.format(new Date()).split(" ")[0];
        String currentTime=sim.format(new Date()).split(" ")[1];

        RecordSet re=new RecordSet();
        RecordSet re1=new RecordSet();
        RecordSet re2=new RecordSet();
        String requestid=requestInfo.getRequestid();
        String workflowid=requestInfo.getWorkflowid();
        String formid= BaseUtil.getFormidId(workflowid);
        String mainbjldw="";
        MainTableInfo mainTableInfo=requestInfo.getMainTableInfo();
        Property[] Property=mainTableInfo.getProperty();
        for(Property prop:Property){
           String name= prop.getName();
           String value= prop.getValue();
           if("bjldw".equalsIgnoreCase(name)){
               mainbjldw=value;
           }
        }
        log.writeLog(mainbjldw+"开始总经理 RewardLeve1Action "+requestid);
        int lastopter= Util.getIntValue(requestInfo.getLastoperator(),0) ;//提交人员
        String lastopterLastname=new User(lastopter).getLastname();//提交人员姓名
        Map<String,String> MainINfo=BaseUtil.getMainRequestbaseByreq(requestid);

        String mainRequestid=MainINfo.get("mainrequestid");//主流程ID
        String mainrequestmark=MainINfo.get("mainrequestmark");//主流程编号
        String mainformid= BaseUtil.getFormidIdByrequestid(mainRequestid);
        Map<String,String> MainData=getMainData(mainRequestid,mainbjldw,mainformid);
        log.writeLog("MainINfo"+MainINfo.toString());
        log.writeLog("MainData"+MainData.toString());
        String nd=MainData.get("nf");//年度
        String yf=MainData.get("yf");//月份
        String mainSqdw=MainData.get("sqdw");
        double qzjldwjezj=Util.getDoubleValue(MainData.get("qzjldwjezj"),0);
        double zcldjljehj=Util.getDoubleValue(MainData.get("zcldjljehj"),0);
        String Mainrequestid=Util.null2String(MainData.get("Mainrequestid")).trim();
        String jlfpbz=Util.null2String(MainData.get("jlfpbz")).trim();
        //设置第一个明细表数据
        String  lsh="";//审批单号
        int fjlx;//分解类型
        String sqrq="";//申请日期
        int sqdw=-1;//申请单位
        int  sqr;//申请人
        String ndjhxm="";//年度计划-项目
        double ndjhys;//年度计划-预算
        double ndjhsyed;//年度计划-剩余额度
        double  sqjlje=Util.getDoubleValue(MainData.get("sqjlje"),0);;//申请奖励金额
        //double qzjldwjezj;//其中奖励单位金额合计
        //double zcldjljehj;//中层领导奖励金额合计
        String jlxmmc="";//奖励项目名称
        double hrfbdpje;//划入分部大盘金额
        int bjldwfb=-1;//被奖励单位（分部）
        double jljefb;//奖励金额（分部）
        double qzzcldjlje;//其中中层领导奖励金额
        double qzjldwje;//其中奖励单位金额
        //奖励分配备注  暂定

        Reward reward=null;
        List<Object> list=new ArrayList<Object>();
        //奖励项目名称(主表)
        String sql="select * from formtable_main_"+formid+" a,formtable_main_"+formid+"_dt1 b  where a.id=b.mainid and a.requestid='"+requestid+"' and a.sffjzcjcshgr='0'";//单位分解
        log.writeLog("sql"+sql);
        re.execute(sql);
        while(re.next()){
            reward=new Reward();
            //奖励项目名称(主表)
            String tsfyxr=Util.null2String(re.getString("tsfyxr")).trim();
            if("0".equals(tsfyxr)){
                continue;
            }
            lsh=Util.null2String(re.getString("spdh")).trim(); //主流程编号
            fjlx=Util.getIntValue(re.getString("sffjzcjcshgr"),-1);//分解类型
            sqrq=Util.null2String(re.getString("sqsj")).trim();//申请日期(主表)
            sqdw=Util.getIntValue(mainSqdw,-1);//Util.getIntValue(re.getString("bjldw"),-1);//申请单位(主表)
            sqr=Util.getIntValue(re.getString("sqr"),-1);//申请人(主表)
            ndjhxm=Util.null2String(re.getString("jlxmmc")).trim();//年度计划-项目(主表)
            ndjhys=Util.getDoubleValue(re.getString("ndys"),0);//年度计划-预算(主表)
            ndjhsyed=Util.getDoubleValue(re.getString("syed"),0);//年度计划-剩余额度(主表)
            bjldwfb=Util.getIntValue(re.getString("bjldw"),-1);//被奖励单位（分部）
            jlxmmc=Util.null2String(re.getString("jlxmmc1"));//奖励项目名称
            hrfbdpje=Util.getDoubleValue(re.getString("hrfbdp"),0);//划入分部大盘金额
            jljefb=Util.getDoubleValue(re.getString("jlje"),0);//奖励金额（分部）
            qzzcldjlje=Util.getDoubleValue(re.getString("zcldjlje"),0);//其中中层领导奖励金额
            qzjldwje=Util.getDoubleValue(re.getString("qzdwjlje"),0);//其中奖励单位金额
            //sqjlje=Util.getDoubleValue(re.getString("jlje"),0);//申请奖励金额

            //qzjldwjezj=Util.getDoubleValue(re.getString("mx2hj"),0);//其中奖励单位金额合计(主表)
            //zcldjljehj=Util.getDoubleValue(re.getString("mx1hj"),0);//中层领导奖励金额合计(主表)
            //获取明细
            int jldwbm=Util.getIntValue(re.getString("bjlbm"),-1);//奖励单位（部门）
            jldwbm=Util.getIntValue(BaseUtil.getTopDepBydepid(jldwbm+""),-1);//获取顶级部门
            double dwjljebm=Util.getDoubleValue(re.getString("jljey"),0);//奖励金额
            String dwjlbz=Util.null2String(re.getString("bz"));//科室分解备注

            reward.setJb(1);
            reward.setMainrequestid(Mainrequestid);
            reward.setFjlx(fjlx);
            reward.setNf(nd);
            reward.setYf(yf);
            reward.setLsh(lsh);
            reward.setSqrq(sqrq);
            reward.setSqdw(sqdw);
            reward.setSqr(sqr);
            reward.setNdjhxm(ndjhxm);
            reward.setNdjhys(ndjhys);
            reward.setNdjhsyed(ndjhsyed);
            reward.setSqjlje(sqjlje);
            reward.setJlxmmc(jlxmmc);
            reward.setHrfbdpje(hrfbdpje);
            reward.setJljefb(jljefb);
            reward.setQzzcldjlje(qzzcldjlje);
            reward.setQzjldwje(qzjldwje);
            reward.setBjldwfb(bjldwfb);
            reward.setQzjldwjezj(qzjldwjezj);
            reward.setZcldjljehj(zcldjljehj);

            reward.setJldwbm(jldwbm);
            reward.setDwjljebm(dwjljebm);
            reward.setDwjlbz(dwjlbz);
            reward.setJlfpbz(jlfpbz);

            reward.setWfrequestid(requestid);
            reward.setTragwfreq(mainRequestid);
            reward.setWfrequestmark(lsh);
            reward.setMainrequestid(mainRequestid);
            reward.setModedatacreatertype("0");
            reward.setModedatacreater("1");
            reward.setModedatacreatedate(currentDate);
            reward.setModedatacreatetime(currentTime);
            list.add(reward);
        }
        //第二个明细
        String sql1="select * from formtable_main_"+formid+" a,formtable_main_"+formid+"_dt2 b  where a.id=b.mainid and a.requestid='"+requestid+"' and a.sffjzcjcshgr='1'";//个人分解
        log.writeLog("sql"+sql1);
        re1.execute(sql1);
        while(re1.next()){
            reward=new Reward();
            //奖励项目名称(主表)
            String tsfyxr=Util.null2String(re1.getString("tsfyxr")).trim();
            if("0".equals(tsfyxr)){
                continue;
            }
            lsh=Util.null2String(re1.getString("spdh")).trim(); //主流程编号
            fjlx=Util.getIntValue(re1.getString("sffjzcjcshgr"),-1);//分解类型
            sqrq=Util.null2String(re1.getString("sqsj")).trim();//申请日期(主表)
            sqdw=Util.getIntValue(mainSqdw,-1);//Util.getIntValue(re1.getString("bjldw"),-1);//申请单位(主表)
            sqr=Util.getIntValue(re1.getString("sqr"),-1);//申请人(主表)
            ndjhxm=Util.null2String(re1.getString("jlxmmc")).trim();//年度计划-项目(主表)
            ndjhys=Util.getDoubleValue(re1.getString("ndys"),0);//年度计划-预算(主表)
            ndjhsyed=Util.getDoubleValue(re1.getString("syed"),0);//年度计划-剩余额度(主表)
            bjldwfb=Util.getIntValue(re1.getString("bjldw"),-1);//被奖励单位（分部）
            jlxmmc=Util.null2String(re1.getString("jlxmmc1"));//奖励项目名称
            hrfbdpje=Util.getDoubleValue(re1.getString("hrfbdp"),0);//划入分部大盘金额
            jljefb=Util.getDoubleValue(re1.getString("jlje"),0);//奖励金额（分部）
            qzzcldjlje=Util.getDoubleValue(re1.getString("zcldjlje"),0);//其中中层领导奖励金额
            qzjldwje=Util.getDoubleValue(re1.getString("qzdwjlje"),0);//其中奖励单位金额
            //sqjlje=Util.getDoubleValue(re1.getString("jlje"),0);//申请奖励金额

            //获取明细
            int jldwbm=Util.getIntValue(re1.getString("bkhks"),-1);//被奖励单位（部门）
            jldwbm=Util.getIntValue(BaseUtil.getTopDepBydepid(jldwbm+""),-1);//获取顶级部门
            int xm=Util.getIntValue(re1.getString("bkhr"),-1);//姓名
            double grjlje=Util.getDoubleValue(re1.getString("bkhje"),0);//奖励金额
            String grjlbz=Util.null2String(re1.getString("bz"));//个人分解备注


            reward.setJb(1);
            reward.setFjlx(fjlx);
            reward.setNf(nd);
            reward.setYf(yf);
            reward.setMainrequestid(Mainrequestid);
            reward.setLsh(lsh);
            reward.setSqrq(sqrq);
            reward.setSqdw(sqdw);
            reward.setSqr(sqr);
            reward.setNdjhxm(ndjhxm);
            reward.setNdjhys(ndjhys);
            reward.setNdjhsyed(ndjhsyed);
            reward.setSqjlje(sqjlje);
            reward.setJlxmmc(jlxmmc);
            reward.setHrfbdpje(hrfbdpje);
            reward.setJljefb(jljefb);
            reward.setQzzcldjlje(qzzcldjlje);
            reward.setQzjldwje(qzjldwje);
            reward.setBjldwfb(bjldwfb);
            reward.setQzjldwjezj(qzjldwjezj);
            reward.setZcldjljehj(zcldjljehj);

            reward.setJldwbm(jldwbm);
            reward.setXm(xm);
            reward.setGrjlje(grjlje);
            reward.setGrjlbz(grjlbz);
            //reward.setDwjlbz(dwjlbz);
            reward.setJlfpbz(jlfpbz);

            reward.setWfrequestid(requestid);
            reward.setMainrequestid(mainRequestid);
            reward.setTragwfreq(mainRequestid);
            reward.setWfrequestmark(lsh);
            reward.setModedatacreatertype("0");
            reward.setModedatacreater("1");
            reward.setModedatacreatedate(currentDate);
            reward.setModedatacreatetime(currentTime);
            list.add(reward);

        }
        //第三个明细 大盘分解
        String sql2="select * from formtable_main_"+formid+" a  where  a.requestid='"+requestid+"' and a.sffjzcjcshgr='2'";//个人分解
        re2.execute(sql2);
        while(re2.next()){
            reward=new Reward();
            //奖励项目名称(主表)
            String tsfyxr=Util.null2String(re2.getString("tsfyxr")).trim();
            if("0".equals(tsfyxr)){
                //continue;
            }
            lsh=Util.null2String(re2.getString("spdh")).trim(); //主流程编号
            fjlx=Util.getIntValue(re2.getString("sffjzcjcshgr"),-1);//分解类型
            sqrq=Util.null2String(re2.getString("sqsj")).trim();//申请日期(主表)
            sqdw=Util.getIntValue(mainSqdw,-1);//Util.getIntValue(re1.getString("bjldw"),-1);//申请单位(主表)
            sqr=Util.getIntValue(re2.getString("sqr"),-1);//申请人(主表)
            ndjhxm=Util.null2String(re2.getString("jlxmmc")).trim();//年度计划-项目(主表)
            ndjhys=Util.getDoubleValue(re2.getString("ndys"),0);//年度计划-预算(主表)
            ndjhsyed=Util.getDoubleValue(re2.getString("syed"),0);//年度计划-剩余额度(主表)
            bjldwfb=Util.getIntValue(re2.getString("bjldw"),-1);//被奖励单位（分部）
            jlxmmc=Util.null2String(re2.getString("jlxmmc1"));//奖励项目名称
            hrfbdpje=Util.getDoubleValue(re2.getString("hrfbdp"),0);//划入分部大盘金额
            jljefb=Util.getDoubleValue(re2.getString("jlje"),0);//奖励金额（分部）
            qzzcldjlje=Util.getDoubleValue(re2.getString("zcldjlje"),0);//其中中层领导奖励金额
            qzjldwje=Util.getDoubleValue(re2.getString("qzdwjlje"),0);//其中奖励单位金额
            //sqjlje=Util.getDoubleValue(re2.getString("jlje"),0);//申请奖励金额
            reward.setJb(1);
            reward.setNf(nd);
            reward.setFjlx(fjlx);
            reward.setYf(yf);
            reward.setLsh(lsh);
            reward.setSqrq(sqrq);
            reward.setSqdw(sqdw);
            reward.setSqr(sqr);
            reward.setNdjhxm(ndjhxm);
            reward.setNdjhys(ndjhys);
            reward.setNdjhsyed(ndjhsyed);
            reward.setSqjlje(sqjlje);
            reward.setJlxmmc(jlxmmc);
            reward.setHrfbdpje(hrfbdpje);
            reward.setJljefb(jljefb);
            reward.setQzzcldjlje(qzzcldjlje);
            reward.setQzjldwje(qzjldwje);
            reward.setBjldwfb(bjldwfb);
            reward.setQzjldwjezj(qzjldwjezj);
            reward.setZcldjljehj(zcldjljehj);
            reward.setJlfpbz(jlfpbz);

            reward.setWfrequestid(requestid);
            reward.setMainrequestid(mainRequestid);
            reward.setTragwfreq(mainRequestid);
            reward.setWfrequestmark(lsh);
            reward.setModedatacreatertype("0");
            reward.setModedatacreater("1");
            reward.setModedatacreatedate(currentDate);
            reward.setModedatacreatetime(currentTime);
            list.add(reward);
        }

        log.writeLog(list.toString());
        String mess="";
        if(list.size()>0){
            RewardBaseService RewardBaseService=new RewardBaseService();
            mess=Util.null2String(RewardBaseService.getObjMapList(list,null)) ;
        }
        if(!"".equals(mess)){
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessage("10001");//提醒信息id
            requestInfo.getRequestManager().setMessagecontent("写入建模失败：请联系管理员"+mess);//提醒信息内容
            return Action.FAILURE_AND_CONTINUE;
        }else{
            re1.execute("update formtable_main_"+formid+" set tsfyxr='0' where requestid='"+requestid+"'");
            updModePimion(requestid,46);
        }
        return super.execute(requestInfo);
    }
    /*
    更新建模权限
     */
    public void updModePimion(String wfrequestid,int modelid){
        RecordSet re=new RecordSet();
        re.execute("select * from uf_zjljlhzbb where wfrequestid='"+wfrequestid+"' ");
        while(re.next()){
            int dataid=re.getInt("id");
            ModeRightInfo mri=new ModeRightInfo();
            mri.editModeDataShare(1,modelid , dataid);
        }
    }
    /*
    获取主流的部分数据  年度 月份 其中奖励单位金额总计 中层领导奖励金额合计
    存在特殊请款  早不到数据 则  取流程数据
     */
    public Map<String,String> getMainData(String mainrequestid,String bjldw,String mainformid){
        Map<String,String> map=new HashMap<String,String>();
        RecordSet re=new RecordSet();
        String sql="select * from  uf_zjljlhzbb  where  wfrequestid='"+mainrequestid+"' and bjldwfb='"+bjldw+"' and isnull(jb,-1)=0";
        re.execute(sql);
        String nf="";
        String yf="";
        String qzjldwjezj="0";
        String zcldjljehj="0";
        String sqjlje="0";
        String Mainrequestid="";
        String jlfpbz="";
        String sqdw="";
        if(re.next()){
            nf=Util.null2String(re.getString("nf")).trim() ;
            yf=Util.null2String(re.getString("yf")).trim();
            Mainrequestid=Util.null2String(re.getString("Mainrequestid")).trim();
            qzjldwjezj=Util.getDoubleValue(re.getString("qzjldwjezj"),0)+"";
            zcldjljehj=Util.getDoubleValue(re.getString("zcldjljehj"),0)+"";
            sqjlje=Util.getDoubleValue(re.getString("sqjlje"),0)+"";
            jlfpbz=Util.null2String(re.getString("jlfpbz"))+"";
            sqdw=Util.null2String(re.getString("sqdw"))+"";

        }else{//取流程数据
            String mainsql="select a.requestid,a.mx1hj,a.mx2hj,a.qjje1,b.bz from formtable_main_"+mainformid+" a,formtable_main_"+mainformid+"_dt2 b  where a.id=b.mainid and a.requestid='"+mainrequestid+"' and b.bjldw='"+bjldw+"' ";
            log.writeLog(mainsql);
            RecordSet mainRec=new RecordSet();
            mainRec.execute(mainsql);
            if(mainRec.next()){
                Map<String,String> nfmap=getNdYf(mainrequestid);
                nf=Util.null2String(nfmap.get("nf")).trim() ;
                yf=Util.null2String(nfmap.get("yf")).trim();
                Mainrequestid=Util.null2String(mainRec.getString("requestid")).trim();
                qzjldwjezj=Util.getDoubleValue(mainRec.getString("mx2hj"),0)+"";
                zcldjljehj=Util.getDoubleValue(mainRec.getString("mx1hj"),0)+"";
                sqjlje=Util.getDoubleValue(mainRec.getString("qjje1"),0)+"";
                jlfpbz=Util.null2String(mainRec.getString("bz"))+"";
                sqdw=Util.null2String(re.getString("sqdw"))+"";
            }
        }
        map.put("nf",nf);
        map.put("yf",yf);
        map.put("qzjldwjezj",qzjldwjezj);
        map.put("zcldjljehj",zcldjljehj);
        map.put("sqjlje",sqjlje);
        map.put("Mainrequestid",Mainrequestid);
        map.put("jlfpbz",jlfpbz);
        map.put("sqdw",sqdw);
        return map;
    }
    /*
    获获取主流程的年份月结  处理特殊情况
     */
    public Map<String,String> getNdYf(String requestid){
        RecordSet re=new RecordSet();
        Map<String,String> map=new HashMap<String,String>();
        String sql="select distinct nf,yf from  uf_zjljlhzbb where wfrequestid='"+requestid+"'";
        re.execute(sql);
        if(re.next()){
                String nf=Util.null2String(re.getString("nf")).trim();
                String yf=Util.null2String(re.getString("yf")).trim();
                map.put("nf",nf);
                map.put("yf",yf);
        }
        return map;
    }
}
